from django import forms
from django.core.exceptions import ValidationError
from user.models import VSession, User, WarZone, Company
from notice.models import NoticeInfo
import pdb


def token_check(value):
    # 验证token必须存在
    sessionList = VSession.objects.filter(token=value)
    if not sessionList.exists():
        errMsg = 'token Error'
        isValidOk = False
        raise ValidationError(errMsg)
    else:
        session = sessionList[0]


def warZoneId_check(value):
    # 验证战区id必须存在
    if value != -1:
        is_num_by_except(value)
        warZone = WarZone.objects.filter(pk=value)
        if not warZone.exists():
            errMsg = 'warZoneId Error'
            raise ValidationError(errMsg)


def companyId_check(value):
    # 验证单位id必须存在
    if value != -1:
        is_num_by_except(value)
        company = Company.objects.filter(pk=value)
        if not company.exists():
            errMsg = 'companyId Error'
            raise ValidationError(errMsg)


def maxItem_check(value):
    if value < 1:
        errMsg = 'maxItem Error'
        raise ValidationError(errMsg)


def page_check(value):
    if value < 1:
        errMsg = 'page Error'
        raise ValidationError(errMsg)


def is_num_by_except(num):
    try:
        int(num)
        return True
    except ValueError:
        errMsg = 'value not int'
        raise ValidationError(errMsg)


class GetIPListForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])


class IPLogScreenForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])
    warZoneId = forms.IntegerField(validators=[warZoneId_check], required=False)
    companyId = forms.IntegerField(validators=[companyId_check], required=False)
    username = forms.CharField(max_length=20, required=False)
    usertype = forms.CharField(max_length=20, required=False)


class GetLogForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])
    logType = forms.CharField(max_length=20)

    def clean(self):
        logType = self.cleaned_data['logType']
        if logType != '' and (logType not in ['0', '1']):
            errMsg = 'type Error'
            raise ValidationError(errMsg)
        return self.cleaned_data


class LogScreenForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])
    logType = forms.CharField(max_length=20)
    name = forms.CharField(max_length=20, required=False)
    startDate = forms.CharField(max_length=20, required=False)
    endDate = forms.CharField(max_length=20, required=False)

    def clean(self):
        logType = self.cleaned_data['logType']
        if logType != '' and (logType not in ['0', '1']):
            errMsg = 'type Error'
            raise ValidationError(errMsg)
        return self.cleaned_data


class LogCheckForm(forms.Form):
    token = forms.CharField(required=False, max_length=100, validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])
    logType = forms.CharField(max_length=20)
    id = forms.CharField(max_length=20, required=False)
    storehouseName = forms.CharField(max_length=20, required=False)
    storeIP = forms.CharField(max_length=20, required=False)
    storePort = forms.CharField(max_length=20, required=False)

    def clean(self):
        logType = self.cleaned_data['logType']
        if logType != '' and (logType not in ['0', '1']):
            errMsg = 'type Error'
            raise ValidationError(errMsg)
        return self.cleaned_data
